package com.hornsun.Quartz;

/**
 * Created by 编译中... on 2018/6/28.
 */

import com.hornsun.config.ConfigInfo;
import com.hornsun.config.IRedisService;
import com.hornsun.data.dbo.*;
import com.hornsun.data.repository.AuthorizationRepository;
import com.hornsun.util.SerializeUtils;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Set;


@DisallowConcurrentExecution
public class OnlineFormJob implements Job {

    @Autowired
    private IRedisService service;

    @Autowired
    private AuthorizationRepository authorizationRepository;

    @Autowired
    private ConfigInfo configInfo;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        Set<String> accounts = service.getKeys(configInfo.getRedisAccount());
        for (String account : accounts){
            byte[] clientInfoBytes = (byte[]) service.get(configInfo.getRedisAccount(),account);
            ClientInfo clientInfo = (ClientInfo) SerializeUtils.deSerialize(clientInfoBytes);
            List<OfflineInfo> offlineInfoList = clientInfo.getOfflineInfoList();
            List<AccountInfo> accountInfoList = clientInfo.getAccountInfoList();
            Authorization authorization = clientInfo.getAuthorization();
            if (authorization==null){
                if(!authorizationRepository.whetherAuthByName(account)){
                    service.remove(configInfo.getRedisAccount(),account);
                    continue;
                }else {
                    clientInfo.setAuthorization(authorizationRepository.getByName(account));
                }
            }
            boolean change = false;
            for (int i =0;i<accountInfoList.size();i++) {
                Date now = new Date();
                if (now.getTime() - accountInfoList.get(i).getTime().getTime() > 25 * 1000) {
                    if (authorization.getOfflineTime() > 0) {
                        OfflineInfo offlineInfo = new OfflineInfo();
                        offlineInfo.setUrl(accountInfoList.get(i).getUrl());
                        offlineInfo.setMac(accountInfoList.get(i).getMac());
                        offlineInfo.setTime(new Date());
                        offlineInfoList.add(offlineInfo);
                        accountInfoList.remove(accountInfoList.get(i));
                    } else {
                        accountInfoList.remove(accountInfoList.get(i));
                    }
                    change = true;
                }
            }
            if (change){
                if (accountInfoList.size()==0&&offlineInfoList.size()==0){
                    service.remove(configInfo.getRedisAccount(),account);
                }else {
                    service.put(configInfo.getRedisAccount(),account, SerializeUtils.serialize(clientInfo),-1);
                }

            }



        }



    }


}
